package org.wjw.gift.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.wjw.base.exception.BusinessException;
import org.wjw.base.exception.ErrorCode;
import org.wjw.base.exception.WjwAssert;
import org.wjw.base.util.AjaxResult;
import org.wjw.base.util.PageList;
import org.wjw.gift.client.AuthClient;
import org.wjw.gift.domain.Employee;
import org.wjw.gift.domain.LoginUser;
import org.wjw.gift.domain.Tenant;
import org.wjw.gift.mapper.EmployeeMapper;
import org.wjw.gift.mapper.TenantMapper;
import org.wjw.gift.query.TenantQuery;
import org.wjw.gift.service.ITenantService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import javax.swing.text.html.parser.Entity;
import java.util.Date;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wjw
 * @since 2023-04-24
 */
//高级查询--关联查询版本
@Service
public class TenantServiceImpl extends ServiceImpl<TenantMapper, Tenant> implements ITenantService {
@Autowired
TenantMapper tenantMapper;
@Autowired
EmployeeMapper employeeMapper;
@Autowired
AuthClient authClient;
    @Override
    public PageList loadPageList(TenantQuery query) {
        //获取分页查询条件
        Page<Tenant> tenantPage = new Page<>(query.getPage(), query.getRows());
        List<Tenant> tenantList = tenantMapper.loadPageList(tenantPage,query);
        return new PageList(tenantPage.getTotal(),tenantList);
    }

    @Override
    public AjaxResult settlement(Tenant tenant) {
        //校验密码
        Employee admin = tenant.getAdmin();
//        if (!admin.getPassword().equals(admin.getConfirmPassword())){
////            throw new BusinessException("两次密码bu一致");
//        }
       // WjwAssert.isEqual(admin.getPassword(),admin.getConfirmPassword(),"两次密码bu一致");
        WjwAssert.isEqual(admin.getPassword(),admin.getConfirmPassword(),ErrorCode.CODE_400_TENANT_ADMIN_PASSWORD_NOT_EQ);
        //通过租户名字找
        List<Tenant> tenants = tenantMapper.selectList(new EntityWrapper<Tenant>()
                .eq("company_name", tenant.getCompanyName()));
//        if (tenants!=null||tenants.size()>0){
//            throw new BusinessException("店铺已存在");
//        }
//        WjwAssert.isNull(tenants,"店铺已存在");
        WjwAssert.isNull(tenants, ErrorCode.CODE_400_TENANT_EXSIT);
        String username = admin.getUsername();
        //去数据库查看此人是否存在
        List<Employee> employees = employeeMapper.selectList(new EntityWrapper<Employee>().
                eq("username", username));
//        if (employees!=null||employees.size()>0){
//            throw new BusinessException("管理员已存在");
//        }
//        WjwAssert.isNull(employees,"管理员已存在");
        WjwAssert.isNull(employees,ErrorCode.CODE_400_TENANT_ADMIN_EXSIT);

        //百度审核
        boolean auditResult =true;
        String message = "审核通过";
        //远程调用，新增loginUser表
        LoginUser loginUser = new LoginUser();
        loginUser.setUsername(admin.getUsername());
        loginUser.setPassword(admin.getPassword());
        loginUser.setTel(admin.getTel());
        loginUser.setEmail(admin.getEmail());
        loginUser.setType(0);
        AjaxResult ajaxResult = authClient.addOrUpdate(loginUser);
        if (!ajaxResult.isSuccess())
            return ajaxResult;
        Long loginId = Long.valueOf(ajaxResult.getResultObj().toString());
         //新增员工表
        admin.setRealName(admin.getUsername());
        admin.setInputTime(new Date());
        admin.setState(0);
        admin.setType(1); //0管理员  1店铺管理员 3店员
        admin.setLoginId(loginId);
        employeeMapper.insert(admin);
        //新增租户表
        tenant.setAdmin(admin);
        tenant.setAdminId(admin.getId());
        tenant.setRegisterTime(new Date());
        tenant.setState(auditResult?1:2);
        tenantMapper.insert(tenant);
        //新增租户菜单中间表
        tenantMapper.saveTenantMeal(tenant.getId(),tenant.getMealId());
        return AjaxResult.me();
    }
}
